package com.lolaage.tracks;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.lolaage.tracks.adapter.PathOverlay;
import com.lolaage.tracks.util.LocationUtils;


/*********************************************************
 * @author : zhaohua
 * @version : 2012-9-17
 * @see : 轨迹的地图视图
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class TrackActivity extends MainMapActivity implements TrackListener
{
    private static final String TAG_SPEC_TRACK_ID = "_spec_track_id";
    private PathOverlay mPathOvLay;
    private MyTrackManager mTrackClient;
    private long mViewTrackId = -1;
    
    @Override
    protected void onCreate(Bundle arg0)
    {
        super.onCreate(arg0);
        if(!handIntent())
        {
            finish();
            return;
        }
        if(isDefaultMapLay())
        {
            setContentView(R.layout.activity_map);
        }
    }
    
    @Override
    public void setContentView(int layoutResID)
    {
        super.setContentView(layoutResID);
        mPathOvLay = new PathOverlay(this);
        mTrackClient = MyTrackManager.getInstance();
        showPath(true);
    }
    
    @Override
    protected void onResume()
    {
        super.onResume();        
        mTrackClient.registerTrackListener(this);
        loadTrack();
    }
    
    @Override
    protected void onPause()
    {
        mTrackClient.unregisterTrackListener(this);
        super.onPause();
    }
    
    /** 是否采用默认的地图布局，
     *  如果用自定义的则必须包含@+id/bmapView的com.baidu.mapapi.MapView的控件*/
    protected boolean isDefaultMapLay()
    {
        return true;
    }
    
    private boolean handIntent()
    {
        Intent it = getIntent();
        mViewTrackId = it.getLongExtra(TAG_SPEC_TRACK_ID, -1);
        return true;
    }
    
    /** 载入轨迹，有指定则载入指定，没有则载入当前*/
    protected void loadTrack()
    {        
        mPathOvLay.clearPoints();
        if(mViewTrackId > 0)
        {
            mTrackClient.loadTrack(mViewTrackId);
        }else
        {
            mTrackClient.loadCurTrack();
        }
    }   
    /** 是否显示线路*/
    public void showPath(boolean visible)
    {
        if(visible)
        {
            if(!mMapView.getOverlays().contains(mPathOvLay))
            {
                mMapView.getOverlays().add(mPathOvLay);
            }
        }else
        {
            if(mMapView.getOverlays().contains(mPathOvLay))
            {
                mMapView.getOverlays().remove(mPathOvLay);
            }
        }
    }
    /** 移动到起点*/
    public void moveToFirst()
    {
        LocationUtils.moveTo(mMapView, mPathOvLay.getStartGP(), true, true);
    }
    /** 移动到终点*/
    public void moveToLast()
    {
        LocationUtils.moveTo(mMapView, mPathOvLay.getEndGP(), true, true);
    }
    
    @Override
    public void onTrackPointChanged(final List<TrackPoint> appendPoints)
    {
        if(appendPoints != null && appendPoints.size() > 0)
        {
            for(TrackPoint point : appendPoints)
            {
                mPathOvLay.addLocation(point.toGeoPoint());
            }
            runOnUiThread(new Runnable()
            {                
                @Override
                public void run()
                {
                    // 添加的点是否有效，并根据可见性，定位点到中间
                    LocationUtils.moveTo(mMapView, appendPoints.get(appendPoints.size() -1).toGeoPoint(), true, false);
                    mMapView.invalidate();
                }
            });
        }
    }

    @Override
    public void onNewTrack(Track track)
    {
        mPathOvLay.clearPoints();
    }
    
    @Override
    public void onTrackUpdated(Track track)
    {
        
    }
    
    public static void startActivity(Context context)
    {
        Intent it = new Intent(context, TrackActivity.class);
        context.startActivity(it);
    }

    public static void startActivity(Context context,long trackId)
    {
        if(trackId <= 0) return;
        Intent it = new Intent(context, TrackActivity.class);
        it.putExtra(TAG_SPEC_TRACK_ID, trackId);
        context.startActivity(it);
    }
}
